`timescale 1ns / 1ps 
////////////////////////////////////////////////////////////////////////////////// 
// Company:  
// Engineer:  
//  
// Create Date:    18:32:28 03/27/2014  
// Design Name:  
// Module Name:    gamelogic  
// Project Name:  
// Target Devices:  
// Tool versions:  
// Description:  
// 
// Dependencies:  
// 
// Revision:  
// Revision 0.01 - File Created 
// Additional Comments:  
// 
////////////////////////////////////////////////////////////////////////////////// 
 
module gamelogic4( 	clk, 
							up, 
							down, 
							forward, 
							back, 
							reset, 
							enemy1_generation, 
							enemy2_generation, 
							enemy3_generation, 
							enemy4_generation, 
							enemy5_generation, 
							enemy6_generation, 
							bullet_enemy1, 
							bullet_generation, 
							bullet_stationary, 
							bullet_generationleft, 
							bullet_generationright, 
							game_over, 
							lives, 
							level1, 
							level2, 
							level3,
							lvl1,
							lvl2,
							scores, 
						   xpos_central,  
							ypos_central,  
							xpos_enemy1,  
							ypos_enemy1,  
							xpos_enemy2,  
							ypos_enemy2, 
							xpos_enemy3,  
							ypos_enemy3, 
							xpos_enemy4,  
							ypos_enemy4, 
							xpos_enemy5,  
							ypos_enemy5, 
							xpos_enemy6,  
							ypos_enemy6, 
							xpos_bullet,  
							ypos_bullet, 
							xpos_bullet_enemy1,  
							ypos_bullet_enemy1, 
							xpos_bullet_stationary, 
							ypos_bullet_stationary, 
							xpos_bulletleft, 
							ypos_bulletleft, 
							xpos_bulletright, 
							ypos_bulletright, 
							xpos_collision, 
							ypos_collision, 
							xpos_collide_enemy1, 
							ypos_collide_enemy1, 
							xpos_collide_enemy2, 
							ypos_collide_enemy2, 
							xpos_collide_enemy3, 
							ypos_collide_enemy3, 
							xpos_collide_enemy4, 
							ypos_collide_enemy4, 
							xpos_collide_enemy5, 
							ypos_collide_enemy5, 
							xpos_collide_enemy6, 
							ypos_collide_enemy6, 
							
							collide_enemy1, 
							collide_enemy2,
							collide_enemy3, 
							collide_enemy4,
							collide_enemy5, 
							collide_enemy6,
							bullet_on, 
							bullet_on1, 
							collision 
							); 
 
 
input up,down; 					// keyboard controls
input forward,back; 				// keyboard controls
input clk;
input bullet_generationleft;  
input bullet_generationright;  
input reset;							//switch for reset  

 
output reg enemy1_generation; //enemy1 generation signal
output reg enemy2_generation; //enemy2 generation signal
output reg enemy3_generation; 
output reg enemy4_generation; 
output reg enemy5_generation; 
output reg enemy6_generation;
output reg bullet_generation; 	//switch for bullet generation 
output reg bullet_enemy1; 
output reg bullet_stationary;
output reg[9:0] xpos_central, ypos_central; 
output reg[3:0] lives; 
output reg game_over;  
output reg[9:0] xpos_enemy1, ypos_enemy1;           // enemy positions
output reg[9:0] xpos_enemy2, ypos_enemy2; 
output reg[9:0] xpos_enemy3, ypos_enemy3; 
output reg[9:0] xpos_enemy4, ypos_enemy4; 
output reg[9:0] xpos_enemy5, ypos_enemy5; 
output reg[9:0] xpos_enemy6, ypos_enemy6; 
output reg[9:0] xpos_bullet, ypos_bullet; 
output reg[9:0] xpos_bullet_enemy1, ypos_bullet_enemy1; 
output reg[9:0] xpos_collision,ypos_collision;                      // collision positions
output reg[9:0] xpos_collide_enemy1,ypos_collide_enemy1; 
output reg[9:0] xpos_collide_enemy2,ypos_collide_enemy2; 
output reg[9:0] xpos_collide_enemy3,ypos_collide_enemy3; 
output reg[9:0] xpos_collide_enemy4,ypos_collide_enemy4; 
output reg[9:0] xpos_collide_enemy5,ypos_collide_enemy5; 
output reg[9:0] xpos_collide_enemy6,ypos_collide_enemy6; 
output reg[9:0] xpos_bulletleft, ypos_bulletleft; 
output reg[9:0] xpos_bulletright, ypos_bulletright; 
output reg[9:0] xpos_bullet_stationary, ypos_bullet_stationary; 
output reg bullet_on=0; 
output reg bullet_on1=0; 
output reg [15:0] scores;
output reg collision,collide_enemy1,collide_enemy2; 
output reg collide_enemy3,collide_enemy4; 
output reg collide_enemy5,collide_enemy6; 
output reg level1,level2,level3,lvl1,lvl2;
 
wire [9:0] position_x1,position_y1; 
wire [9:0] position_x2,position_y2; 
wire [9:0] position_x3,position_y3; 
wire [9:0] position_x4,position_y4; 
wire [9:0] position_x5,position_y5; 
wire [9:0] position_x6,position_y6; 
wire [9:0] position_x7,position_y7; 
wire [9:0] position_x8,position_y8; 
wire [9:0] position_x9,position_y9; 
wire [9:0] position_x10,position_y10; 
wire [9:0] position_x11,position_y11; 
wire [9:0] position_x12,position_y12; 
wire [9:0] position_x13,position_y13;
wire position_valid1,position_valid2,position_valid3,position_valid4,position_valid5; 
wire position_valid13;
wire position_valid6,position_valid7,position_valid8,position_valid9,position_valid10,position_valid11,position_valid12; 

 
reg [30:0] freq_count_y, freq_count_g, freq_count_l; 
reg [30:0] freq_count_z, freq_count_h, freq_count_m; 
reg [30:0] freq_count_p, freq_count_i, freq_count_n; 
reg [30:0] freq_count_a, freq_count_f, freq_count_k; 
reg [30:0] freq_count_q, freq_count_r,freq_count_d,freq_count_e;
reg [30:0]	freq_count_w;
reg [60:0] freq_count_o; 
reg reset1,reset2,p,t,flag,collide,d,e,j,reset3; 
reg [2:0] boss_counter;
reg     xpos_enemy5_previous;  
reg     xpos_bulletleft_previous;
reg     ypos_bulletleft_previous;
reg     ypos_enemy5_previous;
 
position_generation_rev2	pos_generation(	.clk(clk),
													.new_enemy_1(enemy1_generation),//enemy1 generation signal
													.new_enemy_2(enemy2_generation),//enemy2 generation signal 
													.new_enemy_3(enemy3_generation), 
													.new_enemy_4(enemy4_generation), 
													.new_enemy_5(enemy5_generation), 
													.new_enemy_6(enemy6_generation),
													.new_bullet_1(bullet_generation), 
													.new_bullet_enemy_1(bullet_enemy1),
													.cha_pos_x(xpos_central),			//character position signal
													.cha_pos_y(ypos_central),
													.position_x_1(position_x1),
													.position_y_1(position_y1),
													.position_x_2(position_x2),
													.position_y_2(position_y2), 
													.position_x_3(position_x4),
													.position_y_3(position_y4), 
													.position_x_4(position_x5),
													.position_y_4(position_y5), 
													.position_x_5(position_x6),
													.position_y_5(position_y6), 
													.position_x_6(position_x7),
													.position_y_6(position_y7),
												   .position_bullet_x(position_x3),
													.position_bullet_y(position_y3), 
													.position_bullet_enemy_1_x(position_x13),
													.position_bullet_enemy_1_y(position_y13), 
													
													.position_valid_1(position_valid1), //if enemy is in the right range
													.position_valid_2(position_valid2), //if enemy is in the right range
													.position_valid_3(position_valid3),//if bullet is in the right range 
													.position_valid_enemy_1(position_valid13), 
													.position_valid_4(position_valid4), 
													.position_valid_5(position_valid5), 
													.position_valid_6(position_valid6), 
													.position_valid_7(position_valid7), 
													.new_bullet_cha_horizontal(bullet_on), 
                                       .new_bullet_cha_vertical(bullet_on1), 
                                       .new_bullet_enemy_vertical(bullet_stationary), 
                                       .position_bullet_cha_horizontal_x(position_x8), 
                                       .position_bullet_cha_horizontal_y(position_y8), 
                                       .position_bullet_cha_vertical_x(position_x9), 
                                       .position_bullet_cha_vertical_y(position_y9), 
                                       .position_bullet_enemy_stationary_vertical_x(position_x12), 
                                       .position_bullet_enemy_stationary_vertical_y(position_y12), 
                                       .position_valid_cha_horizontal(position_valid8), 
                                       .position_valid_cha_vertical(position_valid9), 
                                       .position_valid_bullet_enemy_stationary_vertical(position_valid12)	 
													);
 

				 
always@ (posedge clk)  
	begin 
		if(reset)                                     // reset for entire game
			begin 
				xpos_central 	<= 10; 
				ypos_central 	<= 240; 
				xpos_collision <= 580; 
				ypos_collision <= 220; 
				xpos_collide_enemy1 <= 580; 
				ypos_collide_enemy1 <= 220; 
				xpos_collide_enemy2 <= 580; 
				ypos_collide_enemy2 <= 220; 
				scores			<= 16'b0000000000000101; 
				lives				<=4'b0100; 
				game_over		<=0; 
				freq_count_y 	<=0; 
				freq_count_z <=0; 
				freq_count_p <=0; 
				freq_count_a <=0; 
				freq_count_w <=0;
				freq_count_q <=0; 
				freq_count_r <=0;
				freq_count_d <=0;
				freq_count_e <=0;
				freq_count_f <=0;
				freq_count_g <=0;
				freq_count_o <=0;
				collision<=0; 
				collide_enemy1<=0; 
				collide_enemy2<=0; 
				level1<=1; 
				level2<=0; 
				level3<=0;
				enemy1_generation<=0; 
				enemy2_generation<=0; 
				enemy3_generation<=0; 
				enemy4_generation<=0; 
				enemy5_generation<=0; 
				enemy6_generation<=0; 
				bullet_generation<=0; 
				bullet_stationary<=0;
				bullet_enemy1<=0;
				collide<=0; 
				collide_enemy3<=0; 
				collide_enemy4<=0; 
				collide_enemy5<=0; 
				collide_enemy6<=0;
				xpos_bulletleft <= 660; 
				ypos_bulletleft <= 600; 
				xpos_bulletright <= 660; 
				ypos_bulletright <= 600; 
				xpos_bullet_enemy1<= 660;
				ypos_bullet_enemy1<=600;
				xpos_bullet<=660;
				ypos_bullet<=600;
				xpos_bullet_stationary<=600;
				ypos_bullet_stationary<=600;
				lvl1<=0;
				lvl2<=0;
				boss_counter <= 0;
				p<=0; 
				t<=0; 
				d<=0;
				e<=0;
				j<=0;
				flag<=0; 
			end  
	
else if(level1 && ~level2 && ~level3)                  // level 1 begins
	begin 
		 
		if(freq_count_a==800000000)                    // counter for enemy1 generation
		   begin 
				enemy1_generation <= 1;                 
					if(position_valid1)  
						begin 
							xpos_enemy1 <= position_x1;  
							ypos_enemy1 <= position_y1;   
						end 
			end 
 
			if(freq_count_a==800000000 ) 
				begin 
					enemy2_generation <= 1; 
						if(position_valid2)  
							begin 
								xpos_enemy2 <= position_x2;  
								ypos_enemy2 <= position_y2;   
							end 
				end 
		
			if(collide_enemy1 || collide_enemy2)                // after collision signal for bullet-enemy collision
				begin
					xpos_bulletleft <= 660; 
					ypos_bulletleft <= 240;  
				end
			else
				begin
					xpos_bulletleft <= position_x8; 
					ypos_bulletleft <= position_y8;  
				end		 
			
		
			if(collide_enemy1 || collide_enemy2)
				begin
					xpos_bulletright <= 660; 
					ypos_bulletright <= 240;  
				end
			else
				begin
					xpos_bulletright <= position_x9; 
					ypos_bulletright <= position_y9;  
				end		 

			if(freq_count_a==800000000) 
				begin 
					bullet_enemy1 <=1; 
					xpos_bullet_enemy1 <= position_x13; 
					ypos_bullet_enemy1 <= position_y13;  
				end      
		 
		 
		
			if (freq_count_y == 1680000)
				begin
					freq_count_y <= 0; 
				end
			else
					freq_count_y <= freq_count_y + 1;
	
		 
			if (freq_count_z == 100000000) 
				begin
					freq_count_z <= 0; 
				end
			else
					freq_count_z <= freq_count_z + 1;
	 
		 
			if (freq_count_p == 1200000000)
				begin
					freq_count_p <= 0; 
				end
			else
					freq_count_p <= freq_count_p + 1;     
			 
			if (freq_count_a == 800000000)
				begin
					freq_count_a <= 800000000; 
				end
			else
					freq_count_a <= freq_count_a + 1; 
		 
				 
			if (freq_count_w == 400000000)
				begin
					freq_count_w <= 0; 
				end
			else
					freq_count_w <= freq_count_w + 1; 
		 
		
		if (down==1 & freq_count_y ==0)
				ypos_central <= ypos_central + 1; 
		
		if (up==1 & freq_count_y ==0) 
				ypos_central <= ypos_central - 1;  
			 
		if (forward==1 & freq_count_y ==0)
				xpos_central <= xpos_central + 1;  
			 
		if (back==1 & freq_count_y ==0)
				xpos_central <= xpos_central - 2;  
			
		if(ypos_central == 280)
			begin 
				ypos_central <= 279; 
			end 
			 
		else if(ypos_central < 100) 
			begin 
				ypos_central <= 100;		
			end 
			 
		else if(xpos_central == 600)
			begin 
				xpos_central <= 1; 
			end 
		 
		else if(xpos_central < 2) 
			begin 
				xpos_central <= 2;	
			end 
	 
		if(freq_count_z==0) 
			scores <= scores + 16'b0000000000000101; 
			 
		if(lives<1) 
			game_over<=1; 
		 
		if(scores<5) 
			scores <=5; 
			 
		if(scores>200)                                 // condition for going to level 2
			begin 
				level2<=1; 
				level1<=0;
				lvl1<=1;
				lvl2<=0;
				reset1<=1;  
				level3<=0; 
				reset2<=0; 
				enemy1_generation<=0; 
				enemy2_generation<=0; 
				bullet_enemy1<=0;
				bullet_generation<=0;
				collide_enemy1<=0;
				collide_enemy2<=0;
			end 
		 
	// collision detection condition for central character with enemy 1 
	 
	if(((xpos_enemy1 - xpos_central <= 47 && ypos_enemy1 - ypos_central <= 45) && ~collide) || 
		((xpos_central - xpos_enemy1 <= 47 && ypos_central - ypos_enemy1 <= 45) && ~collide)) 
		begin 
			lives<=lives-1; 
			scores<=scores-50; 
			collision<=1; 
			collide<=1; 
			xpos_collision<=xpos_central; 
			ypos_collision<=ypos_central; 
			enemy1_generation<=0; 
			enemy2_generation<=0;  
			bullet_enemy1<=0; 
		end 
	 
	 // collision detection condition for central character with enemy 2 
	 
	if(((xpos_enemy2 - xpos_central <= 47 && ypos_enemy2 - ypos_central <= 48) && ~collide) || 
		((xpos_central - xpos_enemy2 <= 47 && ypos_central - ypos_enemy2 <= 48) && ~collide)) 
		begin 
			lives<=lives-1; 
			scores<=scores-50; 
			collision<=1; 
			collide<=1; 
			enemy2_generation<=0; 
			enemy1_generation<=0; 
			xpos_collision<=xpos_central; 
			ypos_collision<=ypos_central; 
		end 
	
	 // collision detection condition for central character with bullet 1 
	 
	if (( xpos_bullet - xpos_central <= 26 && ypos_bullet - ypos_central <= 17 && ~collide ) ||
		( xpos_bullet - xpos_central <= 26 && ypos_central - ypos_bullet <= 17 && ~collide ))
		begin 
			lives<=lives-1; 
			scores<=scores-50; 
			collision<=1; 
			collide<=1; 
			xpos_collision<=xpos_bullet; 
			ypos_collision<=ypos_bullet; 
			bullet_generation<=0;
		end
	 
	
	 
	if(bullet_generationleft) 
        begin 
            bullet_on<=1; 
        end 
    else
        bullet_on<=0;
		  
	if(bullet_generationright)
		begin 
			bullet_on1<=1; end 
	else
		bullet_on1<=0;
	 

	if(( xpos_enemy1 - xpos_bulletleft <= 25 && ypos_bulletleft - ypos_enemy1 <= 45 ) || 
	    ( xpos_enemy1 - xpos_bulletleft <= 25 && ypos_enemy1 - ypos_bulletleft <= 45 ))
		begin
			scores<= scores + 2; 
			collide_enemy1<=1; 
			xpos_collide_enemy1<=xpos_enemy1; 
			ypos_collide_enemy1<=ypos_enemy1; 
			bullet_on<=0;
			enemy1_generation <= 0;
			ypos_bulletleft <= 600;
		end 
	 
	
	if(( xpos_enemy1 - xpos_bulletright <= 25 && ypos_bulletright - ypos_enemy1 <= 45 ) || 
	    ( xpos_enemy1 - xpos_bulletright <= 25 && ypos_enemy1 - ypos_bulletright <= 45 ))
		begin
			scores<= scores + 2; 
			collide_enemy1<=1; 
			xpos_collide_enemy1<=xpos_enemy1; 
			ypos_collide_enemy1<=ypos_enemy1; 
			bullet_on1<=0;
			enemy1_generation <= 0;
			ypos_bulletright <= 600;
		end
	
	

	if(( xpos_enemy2 - xpos_bulletleft <= 25 && ypos_bulletleft - ypos_enemy2 <= 50 ) || 
    ( xpos_enemy2 - xpos_bulletleft <= 25 && ypos_enemy2 - ypos_bulletleft <= 50)) 
		begin
			scores<= scores + 2; 
			collide_enemy2<=1; 
			xpos_collide_enemy2<=xpos_enemy2; 
			ypos_collide_enemy2<=ypos_enemy2; 
			bullet_on<=0; 
			enemy2_generation <= 0;
			ypos_bulletleft <= 600;
		end 
	 
 
	if(( xpos_enemy2 - xpos_bulletright <= 25 && ypos_bulletright - ypos_enemy2 <= 50 ) || 
    ( xpos_enemy2 - xpos_bulletright <= 25 && ypos_enemy2 - ypos_bulletright <= 50)) 
		begin
			scores<= scores + 2; 
			collide_enemy2<=1; 
			xpos_collide_enemy2<=xpos_enemy2; 
			ypos_collide_enemy2<=ypos_enemy2; 
			bullet_on1<=0; 
			enemy2_generation <= 0;
			ypos_bulletright <= 600;
		end 


	 // after collision situation
	 
	if(collide_enemy1==1) 
		begin 
			if(freq_count_w==0) 
				begin 
					collide_enemy1<=0; 
					freq_count_w<=0; 
				end 
		end  
	 
	if(collide_enemy2==1)
		begin 
			if(freq_count_w==0)
				begin 
					collide_enemy2<=0; 
					freq_count_w<=0; 
				end 
		end  
	 
	if(collision==1) 
		begin 
			if(freq_count_p==0)
				begin 
					xpos_central<=10; 
					ypos_central<=240;   
					collision<=0; 
					collide<=0; 
					freq_count_p<=0; 
				end 
	   end 
 
end	 
		 
	 
 else if (level2 && ~level3 && ~level1) 							//level 2 begins
	begin 

		if (lvl1 && freq_count_d == 600000000)
			begin
				freq_count_d <= 0; 
				d<=1;
			end
		else
			freq_count_d <= freq_count_d + 1;

		if(lvl1 && d) 
			begin
				if( freq_count_d ==0) 
					begin
					lvl1<=0;
					end
				end
 
 if (~lvl1 && level2 && ~level3 && ~level1)           
		begin
			if(reset1 && ~p)                               // auto-reset for level2 using flags
				begin 
						xpos_central<=10; 
						ypos_central<=240; 
						freq_count_g 	<=0; 
						freq_count_h <=0; 
						freq_count_i <=0; 
						freq_count_f <=0; 
						p<=1; 
						collision<=0; 
						collide<=0; 
						flag<=0; 
						xpos_bulletleft <= 660; 
						ypos_bulletleft <= 600; 
						xpos_bulletright <= 660; 
						ypos_bulletright <= 600; 
				end 
 
	else 
		begin 
 
			if(lives<1) 
				game_over<=1;
		 
			if(freq_count_f==800000000 ) 
				begin 
					enemy3_generation <= 1; 
						if(position_valid4)  
							begin 
								xpos_enemy3 <= position_x4;  
								ypos_enemy3 <= position_y4;   
							end 
			 end 
		 
			if(freq_count_f==800000000 ) 
				begin 
					enemy4_generation <= 1; 
						if(position_valid5)  
							begin 
								xpos_enemy4 <= position_x5;  
								ypos_enemy4 <= position_y5;   
							end 
				end 
		  	 
		
			if (freq_count_g == 1680000)
				begin
					freq_count_g <= 0; 
				end
			else
					freq_count_g <= freq_count_g + 1;
	
		 
			if (freq_count_h == 100000000) 
				begin
					freq_count_h <= 0; 
				end
			else
					freq_count_h <= freq_count_h + 1;
	 
	 
			if (freq_count_i == 1200000000)
				begin
					freq_count_i <= 0; 
				end
			else
					freq_count_i <= freq_count_i + 1;     
			 
		if (freq_count_f == 800000000)
			begin
				freq_count_f <= 800000000; 
			end
		else
				freq_count_f <= freq_count_f + 1; 
			
		if (freq_count_q == 400000000)
			begin
				freq_count_q <= 0; 
			end
		else
			freq_count_q <= freq_count_q + 1; 
		 
		
		if (down==1 & freq_count_g ==0)
				ypos_central <= ypos_central + 1; 
		
		if (up==1 & freq_count_g ==0) 
				ypos_central <= ypos_central - 1;  
			 
		if (forward==1 & freq_count_g ==0)
				xpos_central <= xpos_central + 2;  
			 
		if (back==1 & freq_count_g ==0)
				xpos_central <= xpos_central - 3;  
			
		if(ypos_central == 280)  
			begin
				ypos_central <= 279;  
			end
			 
		else if(ypos_central < 100) 
			begin 
				ypos_central <= 100;		
			end 
			 
	 
		else if(xpos_central == 598)
				begin 
					xpos_central <= 1; 
				end 
	 
		else if(xpos_central < 3)
				begin 
					xpos_central <= 3;	
				end 

		
		if(bullet_generationleft) 
        begin 
            bullet_on<=1; 
        end 
		else
				bullet_on<=0;
				
	if(bullet_generationright)
		begin 
			bullet_on1<=1; 
		end 
	else
			bullet_on1<=0;
		
		
	if(collide_enemy3 || collide_enemy4)
		begin
				 xpos_bulletright <= 660; 
				 ypos_bulletright <= 240;  
		end
	else
		begin
				 xpos_bulletright <= position_x9; 
				 ypos_bulletright <= position_y9;  
		end	
		
	 if(collide_enemy3 || collide_enemy4)
			begin
				 xpos_bulletleft <= 660; 
				 ypos_bulletleft <= 240;  
			end
	else
			begin
				 xpos_bulletleft <= position_x8; 
				 ypos_bulletleft <= position_y8;  
			end	 
	 
		if(freq_count_h==0) 
				scores <= scores + 16'b0000000000000101; 
			 
		if(lives<1) 
				game_over<=1; 
		 
		if(scores<5) 
				scores <=5; 
			 
		if(scores>300) 
			begin 
				reset2<=1; 
				level3<=1;  
				level2<=0; 
				level1<=0;
				lvl2<=1;
				enemy3_generation<=0; 
				enemy4_generation<=0; 
				collide_enemy3<=0;
				collide_enemy4<=0;
			end 
		 
			if((xpos_enemy3 - xpos_central <= 40 && ypos_enemy3 - ypos_central <= 32 && ~collide ) ||
				( xpos_central - xpos_enemy3 <= 40 && ypos_central - ypos_enemy3 <= 32 && ~collide ))
					begin 
						lives<=lives-1; 
						scores<=scores-50; 
						collision<=1; 
						collide<=1; 
						flag<=1;  
						xpos_collision<=xpos_central; 
						ypos_collision<=ypos_central; 
						enemy1_generation<=0; 
						enemy2_generation<=0; 
					end 
	 
	if (( xpos_enemy4 - xpos_central <= 49 && ypos_enemy4 - ypos_central <= 43 && ~collide ) ||
		( xpos_central - xpos_enemy4 <= 49 && ypos_central - ypos_enemy4 <= 43 && ~collide ))
			begin 
					lives<=lives-1; 
					scores<=scores-50; 
					collision<=1; 
					collide<=1; 
					flag<=1; 
					enemy3_generation<=0; 
					enemy4_generation<=0; 
					xpos_collision<=xpos_central; 
					ypos_collision<=ypos_central; 
			end 
	 
	 
	if ( xpos_bullet - xpos_central <= 26 && ypos_bullet - ypos_central <= 17 && ~collide ) 
			begin 
				lives<=lives-1; 
				scores<=scores-50; 
				collision<=1; 
				collide<=1; 
				xpos_collision<=xpos_central; 
				ypos_collision<=ypos_central; 
			end
	 
	 
	if(collision==1)
		begin 
			if(freq_count_i==0)
				begin 
					xpos_central<=10; 
					ypos_central<=240;   
					collision<=0; 
					collide<=0; 
					freq_count_i<=0; 
					flag<=0; 
					enemy3_generation<=0; 
					enemy4_generation<=0; 
				end 
		end 
 
	if(( xpos_enemy3 - xpos_bulletleft <= 15 && ypos_bulletleft - ypos_enemy3 <= 45 ) || 
	    ( xpos_enemy3 - xpos_bulletleft <= 15 && ypos_enemy3 - ypos_bulletleft <= 45 ))
			begin
				scores<= scores + 2; 
				collide_enemy3<=1; 
				xpos_collide_enemy3<=xpos_enemy3; 
				ypos_collide_enemy3<=ypos_enemy3; 
				bullet_on<=0;
				enemy3_generation <= 0;
				ypos_bulletleft <= 600;
			end 
	 
	
	if(( xpos_enemy3 - xpos_bulletright <= 15 && ypos_bulletright - ypos_enemy3 <= 45 ) || 
	    ( xpos_enemy3 - xpos_bulletright <= 15 && ypos_enemy3 - ypos_bulletright <= 45 ))
			begin
					scores<= scores + 2; 
					collision<=1; 
					collide_enemy3<=1; 
					xpos_collide_enemy3<=xpos_enemy3; 
					ypos_collide_enemy3<=ypos_enemy3; 
					bullet_on1<=0;
					ypos_bulletright <= 600;
					enemy3_generation <= 0;
			end
	
	
	if(( xpos_enemy4 - xpos_bulletleft <= 15 && ypos_bulletleft - ypos_enemy4 <= 50 ) || 
		( xpos_enemy4 - xpos_bulletleft <= 15 && ypos_enemy4 - ypos_bulletleft <= 50)) 

			begin
				scores<= scores + 2; 
				collide_enemy4<=1; 
				xpos_collide_enemy4<=xpos_enemy4; 
				ypos_collide_enemy4<=ypos_enemy4; 
				bullet_on<=0; 
				enemy4_generation <= 0;
				ypos_bulletleft <= 600;
			end
	 
 
	if(( xpos_enemy4 - xpos_bulletright <= 15 && ypos_bulletright - ypos_enemy4 <= 50 ) || 
		( xpos_enemy4 - xpos_bulletright <= 15 && ypos_enemy4 - ypos_bulletright <= 50)) 
			
			begin
				scores<= scores + 2; 
				collide_enemy4<=1; 
				xpos_collide_enemy4<=xpos_enemy4; 
				ypos_collide_enemy4<=ypos_enemy4; 
				bullet_on1<=0; 
				enemy4_generation <= 0;
				ypos_bulletright <= 600;
			end


	 
	if(collide_enemy3==1)
		begin 
			if(freq_count_q==0)
				begin 
					collide_enemy3<=0; 
					freq_count_q<=0; 
				end 
		end  
	 
	if(collide_enemy4==1)
		begin 
		if(freq_count_q==0)
			begin 
				collide_enemy4<=0; 
				freq_count_q<=0; 
			end 
		end  
 
 
 
end
    end 
		end 
 


 
else if (level3 && ~level1 && ~level2) 
begin 
 
if (lvl2 && freq_count_e == 800000000)
		begin
			freq_count_e <= 0; 
			e<=1;
		end
		else
			freq_count_e <= freq_count_e + 1;

if(lvl2 && e)
	begin
		if( freq_count_e ==0)
			begin
					lvl2<=0;
			end
	end
  
if (~lvl2 && level3 && ~level2 && ~level1)
	begin
 
if(reset2 && ~t)
	begin 
			xpos_central<=10; 
			ypos_central<=240; 

			freq_count_k 	<=0; 
			freq_count_l <=0; 
			freq_count_m <=0; 
			freq_count_n <=0; 
			freq_count_o <=0;
			t<=1; 
			collision<=0; 
			collide<=0; 
			flag<=0; 
			xpos_bulletleft <= 660; 
			ypos_bulletleft <= 600; 
			xpos_bulletright <= 660; 
			ypos_bulletright <= 600; 
	end 
 
else
	begin 
	if(freq_count_k==800000000) 
		   begin 
		enemy5_generation <= 1; 
		xpos_enemy5 <= position_x6;  
		ypos_enemy5 <= position_y6;   
			 
			end 
		
		 
			if(freq_count_k==800000000 ) 
		begin 
			enemy6_generation <= 1; 
			if(position_valid7)  
			begin 
				xpos_enemy6 <= position_x7;  
				ypos_enemy6 <= position_y7;   
			 
			end 
			
		end 
		 
		 
		if(freq_count_k==800000000) 
		   begin 
			bullet_generation <=1; 
			//if(position_valid3)  
			//begin 
				 xpos_bullet <= position_x3; 
				 ypos_bullet <= position_y3;  
		end    
	 
		if(freq_count_k==800000000) begin 
		bullet_stationary<=1; 
	 
			if(position_valid12)  
			begin 
				 xpos_bullet_stationary <= position_x12; 
				 ypos_bullet_stationary <= position_y12;  
				 
			end 
			
  end 
	
		if (freq_count_l == 1680000)
		begin
			freq_count_l <= 0; 
			
		end
		else
			freq_count_l <= freq_count_l + 1;
	
		 
		if (freq_count_m == 100000000) 
		
		begin
			freq_count_m <= 0; 
		
		end
		else
			freq_count_m <= freq_count_m + 1;
	 
		if (freq_count_n == 1200000000)
		begin
			freq_count_n <= 0; 
		
		end
		else
			freq_count_n <= freq_count_n + 1; 
			
			 
		if (freq_count_k == 800000000)
		begin
			freq_count_k <= 800000000; 
		
		end
		else
			freq_count_k <= freq_count_k + 1;

			if (freq_count_r == 400000000)
		begin
			freq_count_r <= 0; 
		
		end
		else
			freq_count_r <= freq_count_r + 1; 
		 
		
		if (down==1 & freq_count_l ==0)
			ypos_central <= ypos_central + 2; 
		
		if (up==1 & freq_count_l ==0) 
			ypos_central <= ypos_central - 2;  
			 
		if (forward==1 & freq_count_l ==0)
			xpos_central <= xpos_central + 3;  
			 
		if (back==1 & freq_count_l ==0)
			xpos_central <= xpos_central - 4;  
			
		if(ypos_central == 280) begin 
			ypos_central <= 279; end 
			 
		else if(ypos_central < 100) begin 
			ypos_central <= 100;		 end 
			 
		else if(xpos_central == 597) begin 
			xpos_central <= 1; end 
		 
		else if(xpos_central < 4) begin 
			xpos_central <= 6;		end 
		
	 
		if(freq_count_m==0) 
			scores <= scores + 16'b0000000000000101; 
			 
		
		if(scores<5) 
			scores <=5; 
		 
	 if( xpos_enemy5 - xpos_central <= 50 && ypos_enemy5 - ypos_central <= 50 && ~collide)
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	collide<=1;
	flag<=1;
	enemy5_generation<=0;
	enemy6_generation<=0;
   end
	 
	if (( xpos_enemy6 - xpos_central <= 50 && ypos_enemy6 - ypos_central <= 50 && ~collide) ||
		( xpos_central - xpos_enemy6 <= 50 && ypos_central - ypos_enemy6 <= 50 && ~collide))
	begin 
	lives<=lives-1; 
	scores<=scores-50; 
	collision<=1; 
	xpos_collision<=xpos_central; 
	ypos_collision<=ypos_central; 
	collide<=1; 
	flag<=1; 
	enemy5_generation<=0; 
	enemy6_generation<=0; 
	end 
	 
	if (( xpos_bullet - xpos_central <= 26 && ypos_bullet - ypos_central <= 17 && ~collide) ||
		( xpos_bullet - xpos_central <= 26 && ypos_central - ypos_bullet <= 17 && ~collide))
	begin 
	lives<=lives-1; 
	scores<=scores-50; 
	collision<=1; 
	xpos_collision<=xpos_central; 
	ypos_collision<=ypos_central; 
	collide<=1;
	end 
	
	if (( xpos_bullet_stationary - xpos_central <= 26 && ypos_bullet_stationary - ypos_central <= 17 && ~collide)|| 
		( xpos_central - xpos_bullet_stationary <= 26 && ypos_central - ypos_bullet_stationary <= 17 && ~collide))
	begin 
	lives<=lives-1; 
	scores<=scores-50; 
	collision<=1; 
	xpos_collision<=xpos_central; 
	ypos_collision<=ypos_central; 
	collide<=1;
	end 
	
	 
	if(collision==1) begin 
   if(freq_count_n==0) begin 
	 xpos_central<=10; 
	 ypos_central<=240;   
		collision<=0; 
		collide<=0; 
		freq_count_n<=0; 
		flag<=0; 
	enemy5_generation<=0; 
	enemy6_generation<=0; 
	   end 
		end 
 
			if(bullet_generationleft) 
        begin 
            bullet_on<=1; 
        end 
    else
        bullet_on<=0;
	if(bullet_generationright) begin 
	bullet_on1<=1; end 
	 else bullet_on1<=0;
		
		if(collide_enemy5 || collide_enemy6)
		begin
				 xpos_bulletright <= 660; 
				 ypos_bulletright <= 240;  
		end
		else
		begin
				 xpos_bulletright <= position_x9; 
				 ypos_bulletright <= position_y9;  
		end	
 
	 if(collide_enemy5 || collide_enemy6)
		begin
				 xpos_bulletleft <= 660; 
				 ypos_bulletleft <= 240;  
		end
		else
		begin
				 xpos_bulletleft <= position_x8; 
				 ypos_bulletleft <= position_y8;  
		end	

     xpos_enemy5_previous       <= xpos_enemy5;  
     xpos_bulletleft_previous   <= xpos_bulletleft;
     ypos_enemy5_previous       <= ypos_enemy5;
     ypos_bulletleft_previous   <= ypos_bulletleft;
 
   //  if(bullet_on) begin
	if((( xpos_enemy5 - xpos_bulletleft <= 15 && ypos_bulletleft - ypos_enemy5 <= 45 ) || 
	    ( xpos_enemy5 - xpos_bulletleft <= 15 && ypos_enemy5 - ypos_bulletleft <= 45 )) && 
        (~(( xpos_enemy5_previous - xpos_bulletleft_previous <= 15 && ypos_bulletleft_previous - ypos_enemy5_previous <= 45 ) || 
                ( xpos_enemy5_previous - xpos_bulletleft_previous <= 15 && ypos_enemy5_previous - ypos_bulletleft_previous <= 45 ))))

	begin
		if( boss_counter == 3)
			begin
				boss_counter <=0;
			//	collision<=1; 
				scores<= scores + 2; 
				collide_enemy5<=1; 
				xpos_collide_enemy5<=xpos_enemy5; 
				ypos_collide_enemy5<=ypos_enemy5; 
				bullet_on<=0;
				enemy5_generation <= 0;
				ypos_bulletleft <= 600;
			end
        else
			begin
				boss_counter <= boss_counter + 1;
				ypos_bulletleft<=600;
			//	bullet_on<=0;
			end
	end
	 
//	if(bullet_on1) begin
	if(( xpos_enemy5 - xpos_bulletright <= 15 && ypos_bulletright - ypos_enemy5 <= 45 ) || 
	    ( xpos_enemy5 - xpos_bulletright <= 15 && ypos_enemy5 - ypos_bulletright <= 45 ))
	begin
	scores<= scores + 2; 
//	collision<=1; 
   collide_enemy5<=1; 
	xpos_collide_enemy5<=xpos_enemy5; 
   ypos_collide_enemy5<=ypos_enemy5; 
	bullet_on1<=0;
  	enemy5_generation <= 0;
	ypos_bulletright <= 600;
	end //end
	
	
//	if(bullet_on) begin
if(( xpos_enemy6 - xpos_bulletleft <= 15 && ypos_bulletleft - ypos_enemy6 <= 50 ) || 
    ( xpos_enemy6 - xpos_bulletleft <= 15 && ypos_enemy6 - ypos_bulletleft <= 50)) 

   begin
	scores<= scores + 2; 
	//collision<=1;
    collide_enemy6<=1; 
   xpos_collide_enemy6<=xpos_enemy6; 
	ypos_collide_enemy6<=ypos_enemy6; 
	bullet_on<=0; 
   enemy6_generation <= 0;
	ypos_bulletleft <= 600;
	end
	
if(( xpos_enemy6 - xpos_bulletright <= 15 && ypos_bulletright - ypos_enemy6 <= 50 ) || 
    ( xpos_enemy6 - xpos_bulletright <= 15 && ypos_enemy6 - ypos_bulletright <= 50)) 

   begin
	scores<= scores + 2; 
    collide_enemy6<=1; 
   xpos_collide_enemy6<=xpos_enemy6; 
	ypos_collide_enemy6<=ypos_enemy6; 
	bullet_on1<=0; 
   enemy6_generation <= 0;
	ypos_bulletright <= 600;
	end 


	 
	if(collide_enemy5==1) begin 
	if(freq_count_r==0) begin 
	collide_enemy5<=0; 
	freq_count_r<=0; 
	end end  
	 
	if(collide_enemy6==1) begin 
	if(freq_count_r==0) begin 
	collide_enemy6<=0; 
	freq_count_r<=0; 
	end end  
 
 
 	if(lives<1) 
			game_over<=1;
 
				end    
			end
		end
	end 
	
endmodule 
